package cn.itcast.controller;

import cn.itcast.domain.Msg;
import cn.itcast.domain.StuAndCourse;
import cn.itcast.domain.Student;
import cn.itcast.domain.Teacher;
import cn.itcast.service.StudentService;
import cn.itcast.service.TeacherService;
import cn.itcast.service.impl.StudentServiceImpl;
import cn.itcast.service.impl.TeacherServiceImpl;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class LoginController {

    @Autowired
    StudentService studentService;
    @Autowired
    Student student;

    @Autowired
    TeacherService teacherService;
    @Autowired
    Teacher teacher;

    @Autowired
    Msg msg = null;

    // 登录
    @RequestMapping(value = "/login" , method = RequestMethod.POST)
    @ResponseBody
    public Msg login(@Param("username") String username, @Param("password") String password, HttpSession session){

        student = studentService.findByStuAndPas(username,password);
        teacher = teacherService.findByStuAndPas(username,password);
        if (student != null ){
           msg = Msg.success().add("depts","1");
           session.setAttribute("student",student);
            session.setAttribute("studentss","1");
        }else if (teacher != null){
            msg = Msg.success().add("depts","2");
            session.setAttribute("teacher",teacher);
            session.setAttribute("teacherss","2");
        }else {
            msg = Msg.fail().add("depts","0");
        }
        return msg;
    }


    @RequestMapping(value = "/end")
    @ResponseBody
    public Msg end(HttpSession session){
//        session.setAttribute("studentss","0");
        session.invalidate();
        msg = Msg.success();
        return msg;
    }



}
